package com.atguigu.service.impl;

import com.atguigu.dao.BaseDao;
import com.atguigu.dao.CommunityDao;
import com.atguigu.dao.DictDao;
import com.atguigu.entity.Community;
import com.atguigu.service.CommunityService;
import com.atguigu.service.ipml.BaseServiceImpl;
import com.atguigu.util.CastUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

@DubboService
public class CommunityServiceImpl extends BaseServiceImpl<Community> implements CommunityService {

    @Autowired //service注入dao用Autowired
    private CommunityDao communityDao;
    @Autowired //注入数字字典
    private DictDao dictDao;


    @Override
    public BaseDao<Community> getDao() {
        return communityDao;
    }


    @Override
    public PageInfo<Community> findPage(Map filters) {
        int pageNum= CastUtil.castInt(filters.get("pageNum"),1);
        int pageSize=CastUtil.castInt(filters.get("pageSize"),5);
        PageHelper.startPage(pageNum,pageSize);
        List<Community> list = communityDao.findPage(filters);
        for (Community community : list) {
            //根据区域id找区域名字
            String areaName = dictDao.getById(community.getAreaId()).getName();
            //根据板块id找板块名字
            String plateName = dictDao.getById(community.getPlateId()).getName();
            //将区域名字和板块名字存到community中
            community.setAreaName(areaName);
            community.setPlateName(plateName);
        }
        return new PageInfo<>(list,3);
    }


    @Override
    public List<Community> findAll() {
        return communityDao.findAll();
    }


    @Override
    public Community getById(Serializable id) {
        Community community = communityDao.getById(id);
        //获取区域名字
         community.setAreaName(dictDao.getById(community.getAreaId()).getName());
        //获取板块名字
        community.setPlateName(dictDao.getById(community.getPlateId()).getName());
        return community;
    }
}